/*
 * @Author Shunzi
 * @Date 2021-07-12 17:11:34
 * @LastEditTime 2021-07-16 00:49:52
 * @LastEditors Shunzi
 * @Description
 * @FilePath /BFFNodeServer/request/index.js
 */
const axios = require("axios");
const { join } = require("path");
const instance = axios.create({
  baseURL: "http://localhost/booklibrary/web/",
  timeout: 1000,
});

instance.interceptors.request.use(
  (config) => {
    return config;
  },
  (error) => {
    return error;
  }
);

instance.interceptors.response.use(
  (response) => {
    if (response.data) {
      return Promise.resolve(response.data);
    }
    return Promise.reject(response);
  },
  (error) => {
    if (~`${error.message}`.indexOf("timeout")) {
      console.log("网关错误");
    }
    error.response?.statusText && console.error(error.response.statusText);
    console.error(error.response);

    return Promise.reject(error);
  }
);

module.exports = {
  get: (url, params) => {
    return instance.request({
      method: "get",
      url,
      params,
    });
  },
  post: (url, data, config) => {
    return instance.request({
      method: "post",
      url,
      data,
      ...config,
    });
  },
  delete: (url, id) => {
    return instance.request({
      method: "delete",
      url,
    });
  },
  update: (url, data) => {
    return instance.request({
      method: "update",
      url,
      data,
    });
  },
};
